package com.lich;

import com.lich.factory.FieldFactory;
import com.lich.factory.MysqlFieldFactory;
import com.lich.factory.PostgresqlFieldFactory;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 实体类创建工具
 * created by lich
 * 2018.12.04
 */
public class EntityBuilder {
    private static Connection con = null;

    /**
     * 获取数据连接
     *
     * @return
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(Constants.DRIVER_CLASS);
        con = DriverManager.getConnection(Constants.DATABASE_URL, Constants.DATABASE_USER, Constants.DATABASE_PASSWORD);
        return con;
    }

    public static void buildEntity(String tableName, String dbType) throws Exception {
        if (CommonUtils.isEmptyString(tableName)) {
            throw new Exception("表名不能为空");
        }
        if (CommonUtils.isEmptyString(dbType)) {
            throw new Exception("数据库类型不能为空");
        }
        FieldFactory fieldFactory = null;
        switch (dbType) {
            case "postgresql":
                fieldFactory = new PostgresqlFieldFactory();
                break;
            case "mysql":
                fieldFactory = new MysqlFieldFactory();
                break;
            default:
                throw new Exception("不支持的数据类型");
        }
        List<Field> fieldList = fieldFactory.getFieldByTableName(getConnection(), tableName);
        if (fieldList.isEmpty()) {
            throw new Exception("表" + tableName + "不存在");
        }
        String className = CommonUtils.upperFirstChar(CommonUtils.camelCaseName(CommonUtils.removePrefixAndSuffix(tableName, Constants.TABLE_PREFIX, Constants.TABLE_SUFFIX)));
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        cfg.addPkg("com.lich");
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate(Constants.TEMPLATE_NAME);
        t.binding("tableName", tableName);
        t.binding("className", className);
        t.binding("fieldList", fieldList);
        String str = t.render();
        System.out.println(str);
        String path = "entity/" + className + ".java";
        File file = new File(path);
        org.apache.commons.io.FileUtils.writeStringToFile(file, str, "UTF-8");
    }

    public static void main(String[] args) {
        try {
            buildEntity(Constants.TABLE_NAME, Constants.DB_TYPE);
            System.out.println("======================================================================");
            System.out.println("  __             _   _            _ _      _       _    _             \n" +
                    " / _|           | | | |          | (_)    | |     | |  (_)            \n" +
                    "| |_ ___  _ __  | |_| |__   ___  | |_  ___| |__   | | ___ _ __   __ _ \n" +
                    "|  _/ _ \\| '__| | __| '_ \\ / _ \\ | | |/ __| '_ \\  | |/ / | '_ \\ / _` |\n" +
                    "| || (_) | |    | |_| | | |  __/ | | | (__| | | | |   <| | | | | (_| |\n" +
                    "|_| \\___/|_|     \\__|_| |_|\\___| |_|_|\\___|_| |_| |_|\\_\\_|_| |_|\\__, |\n" +
                    "                                                                 __/ |\n" +
                    "                                                                |___/ ");
            System.out.println("======================================================================");
            System.out.println("修改配置文件config.properties可以自定义配置");
            System.out.println("======================================================================");
            System.out.println("生成的class文件路径：entity/XXX.java");
            System.out.println("======================================================================");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}